package com.cxhome.cxinfox.cxdepartment;

import java.util.List;

import com.jfinal.plugin.activerecord.Page;
import com.traffic.managent.base.BaseService;
import com.traffic.managent.base.Paginator;
import com.traffic.managent.model.system.Department;
import com.traffic.managent.util.SQLUtils;

/**
 * 数据字典service
 * 
 */
public class DepartmentService extends BaseService {

	public static final DepartmentService service = new DepartmentService();

	public Page<Department> paginate(Department model, Paginator paginate, StringBuilder orderSql) {

		SQLUtils sql = new SQLUtils(
				" from cx_department t " + " left join cx_department p on t.parentdepartmentids = p.ids  where 1=1 ");
		if (model.getAttrValues().length != 0) {
			sql.setAlias("t");
			// 查询条件
			sql.whereLike("names", model.getStr("names"));
		}
		sql.append(orderSql);
		return Department.dao.paginate(paginate, "select t.*,p.names as parentName ", sql.toString());

	}

	public void saveAdd(Department model) {
		Department parentmodel = Department.dao.findFirst("select * from cx_department where 1=1 and ids = ?",
				model.getStr("parentdepartmentids"));
		String pathName = parentmodel.getStr("pathsName");
		String paths = parentmodel.getStr("paths");

		pathName += "/" + model.getStr("names");
		paths += "/" + model.getStr("ids");

		model.put("pathsName", pathName);
		model.put("paths", paths);
		model.save();
	}

	public void saveEdit(Department model) {
		Department parentmodel = Department.dao.findFirst("select * from cx_department where 1=1 and ids = ?",
				model.getStr("parentdepartmentids"));
		String pathName = parentmodel.getStr("pathsName");
		String paths = parentmodel.getStr("paths");

		pathName += "/" + model.getStr("names");
		paths += "/" + model.getStr("ids");

		model.put("pathsName", pathName);
		model.put("paths", paths);
		model.update();
	}

	/**
	 * 获取部门下拉框
	 * 
	 * @return
	 */
	public String selectDepart(String selected) {

		List<Department> list = Department.dao.find(" select * from cx_department where 1=1 and delFlag = '0' ");

		StringBuffer sb = new StringBuffer("");
		if (list != null && list.size() > 0) {
			for (Department department : list) {
				String parentId = department.getStr("parentdepartmentids");
				if ("100".equals(parentId)) {
					String id = department.getStr("ids");
					sb.append("<option value=\"");
					sb.append(id);
					sb.append("\" ");
					boolean flag = (selected != null && id.equals(selected));
					sb.append(flag ? "selected" : "");
					sb.append(">");
					sb.append(department.getStr("names"));
					sb.append("</option>");

					sb.append(recursionTree(id, selected, 1, list));
				}
			}
		}
		return sb.toString();
	}

	/**
	 * 递归树
	 * 
	 */
	public String recursionTree(String rootId, String selected, int level, List<Department> list) {
		if (list == null || list.size() <= 0) {
			return "";
		}
		// 级别太多了
		if (level >= 20) {
			return "";
		}

		StringBuffer sb = new StringBuffer("");
		for (Department department : list) {
			String parentId = department.getStr("parentdepartmentids");

			if (rootId.equals(parentId)) {
				String id = department.getStr("ids");
				sb.append("<option value=\"");
				sb.append(id);
				sb.append("\" ");
				boolean flag = (selected != null && id.equals(selected));
				sb.append(flag ? "selected" : "");
				sb.append(">");
				for (int i = 0; i < level; i++) {
					if (i == (level - 1)) {
						sb.append("&nbsp;|--");
					} else {
						sb.append("&nbsp;&nbsp;&nbsp;");
					}
				}
				sb.append(department.getStr("names"));
				sb.append("</option>");

				sb.append(recursionTree(id, selected, (level + 1), list));
			}
		}

		return sb.toString();
	}

	/**
	 * 获取名称
	 * 
	 */
	public String getDepartName(Integer id) {
		if (id == null || id == 0) {
			return "";
		}
		String name = Department.dao.findById(id).getStr("name");
		return name;
	}

}
